require([ 'jquery', 'common', 'validate','timepicker', 'pagination', 'bootstrap', 'dateZh','plupload','T_upload'], function($, common,validate,timepicker,T_upload) {
	
	//日期控件
	$('#date-start, #date-end').datetimepicker({
		format : "yyyy-mm-dd hh:ii:ss",
		language : "zh-CN",
		todayBtn : 1,
		autoclose : true,
		todayHighlight : true,
		forceParse : true,
		minuteStep : 10
	});
	
	var addForm = $('#addForm');
	var updateForm = $('#updateForm');
	
	var first_load = true;
	var reload = true; 
	
	//placeholder兼容ie start
	$(function() {
		$("img").click(function () {
	        var src = $(this).attr("src");
	        console.log("src:"+src);
	        window.open(src);
	    });
		if (!placeholderSupport()) { // 判断浏览器是否支持 placeholder
			$('[placeholder]').focus(function() {
				var input = $(this);
				if (input.val() == input.attr('placeholder')) {
					input.val('');
					input.removeClass('placeholder');
				}
			}).blur(function() {
				var input = $(this);
				if (input.val() == ''
						|| input.val() == input.attr('placeholder')) {
					input.addClass('placeholder');
					input.val(input.attr('placeholder'));
				}
			}).blur();
		};
		
		showByPage(1); 
	});
	function placeholderSupport() {
		return 'placeholder' in document.createElement('input');
	}
	//placeholder兼容ie end

	//清除按钮
	$('.clearBtn').on('click', function() {
		$('.setTableLength').val(10);
		$("#orderId").val(''),  
		$("select[name='isSolve']") 
	});
	
	$(".setTableLength").change(function() {
		reload = true;
		showByPage(1);
	});  
	$(".searchBtn").click(function() {
		reload = true;
		showByPage(1);
	});
	
	/** 列表查询start **/
	function showByPage(pageNum) {
		if ($(".all").is(':checked')) {
			$(".all").removeAttr('checked');								
		}	
		if($('.alert-warning').length>0){
    		$('.alert-warning').remove();
    	}
		$.getJSON(ctx + '/order/exception/showbypage', {	
				pageNum : pageNum,
				pageSize : $('.setTableLength').val() || 10, 
				orderId : $("#orderId").val(),
				isSolve : $("select[name='isSolve']").val(),
			},
			function(data){  
				if($('.loading')){
					$('.loading').remove();
				}
				var result = data.result;
				if(result!=null && result=="noauthority") {
					common.alert1('你无此权限!');
					return;
				}
				if(data.result!=null && data.result=="againLogin") {
					common.alert1("登录过期，请重新登录");
						setTimeout("location.href = ctx + '/login.html'",1000);
       			} else if(data.result.length == 0) { 
					$('.table-responsive').append('<div class="alert alert-warning no-result text-center" role="alert">没有找到记录 </div>');
                } else{
       				var datapage = data.result;
					var datalist = datapage.result;
					var totalCount = datapage.totalCount;
					var pageNum = datapage.pageNo;
					var pageSize = datapage.pageSize;
					var totalPages = datapage.totalPages;
					$(".totalCount").html(totalCount);
					$(".totalPages").html(totalPages);
					$(".pageNo").html(pageNum);
                    var tempHtml="";   
                    var ser = 1;
                    for(var i=0;i<datalist.length;i++){   
                    	var data = datalist[i]; 
                    	var no = (pageNum-1)*pageSize+i+1; 
                    	tempHtml +="<tr data-id='"+data.id+"' data-pageno='" + pageNum + "'>";
                    	tempHtml +="<td>"+ser+"</td>"; 
                    	tempHtml +="<td>"+isnull(data.orderId)+"</td>"; 
                    	tempHtml +="<td>"+exceptionType(data.exceptionType)+"</td>";  
                    	tempHtml +="<td>"+exceptionImg(data.imgPath)+"</td>";  
                    	tempHtml +="<td>"+isnull(data.remark)+"</td>"; 
                    	tempHtml +="<td>"+isSolve(data.isSolve)+"</td>"; 
                    	tempHtml +="<td>"+common.getSmpFormatDate(data.createTime)+"</td>";   
                    	tempHtml +="<td>";
                    	if(data.isSolve == 1){
                    		tempHtml +="<button type='button' data-id="+data.id+" class='btn btn-primary btn-xs restore-btn'>恢复</button>&nbsp;";
                    	} 
                    	tempHtml +="</td>";
                    	ser = ser + 1; 
                    	tempHtml +="</tr>";
                    	if($('.alert-warning').length>0){
                    		$('.alert-warning').remove();
                    	}
                    }   
                    if(datalist.length === 0&&$('.alert-warning').length<=0) { 
						$('.table-responsive').append('<div class="alert alert-warning no-result text-center" role="alert">没有找到记录 </div>');
	                } 
                    $('#dataBody').html(tempHtml);
                   
                    if (reload) {
                    	reload = false;
                    	first_load = true;
    					var initPagination = function() {
    						$("#Pagination").pagination(totalCount, {
    							num_edge_entries : 1, // 边缘页数
    							num_display_entries : 4, // 主体页数
    							callback: pageselectCallback,
    							prev_text : "«",
    							next_text : "»",
    							items_per_page : pageSize, 
    							link_to : 'javascript:void(0);'
    						});
    					}();
    					/**分页e**/
                    }
				} 
			}).fail(function (err) { 
				if($('.loading')){
					$('.loading').remove();
				}
				common.alert('服务器错误！请刷新页面重试或联系管理员',"确定"); 
            });  
	}
	
	function pageselectCallback(page_index, jq) {
		if (first_load) { //第一次初始化不需要请求
			first_load = false;
			return false;
		}
		pageNum = page_index + 1;
		showByPage(pageNum);
	}
	/** 列表查询end **/ 
	
	/** 恢复start **/
	$(document).on('click', '.restore-btn', function(){
		var varId = $(this).data("id");
		var parentTr = $(this).parents('tr');
        var pageno = parentTr.data('pageno');
		$('.commonConfirm').modal('show');				
		common.commonConfirm("恢复", "确定恢复?", "",
			function() {
            	 $.post(ctx+"/order/exception/restore", {
    				 id:varId
    			 },function (data) {
    				 var result = data.result;
					 if(result!=null && result=="noauthority") {
						 common.alert1('你无此权限!');
						 return;
					 }
    	    		 if(data.result!=null && data.result=="againLogin") {
    	    			 $('.commonConfirm').modal('hide');
    	    			 common.alert1("登录过期，请重新登录");
    					 setTimeout("location.href = ctx + '/login.html'",1000);
    	             }else if(data.result=='success'){ 
    	            	$('.commonConfirm').modal('hide');
    	     	        common.mySuccessModal2(data.message);
    	     	       showByPage(pageno);
    	         	}else{
    	         		common.alert1(data.message);
    	         	}
    	         },"json"); 
			}
		);
	});
	/** 恢复end **/
	
	function isnull(value) {
		if (value == null || value == '' || typeof (value) == "undefined")
			return "--";
		else
			return value;
	} 
	 
	/*是否已解决，1否，2是*/
	function isSolve(val) {
		if(val == null || val == '') {
			return "";
		}else if(val == 1){
			return "<span style='color:red'>未解决</span>";
		}else{
			return "<span style='color:green'>已解决</span>";
		}
	}
	
	function exceptionImg(imgPath) {
		if(imgPath == null || imgPath == '') {
			return "";
		}else{
			return "<a target='_blank' href="+ ctx + imgPath+"><img src="+ctx + imgPath+"  width='20px' /></a>";
		}
	}
	
	/*审核状态，1待审核，2审核通过，3不通过*/
	function auditStatus(value) {
		if (value == null || value == ''){
			return "未知";
		}else if(value == 1) {
			return "待审核";
		}else if(value == 2) {
			return "审核通过";
		}else if(value == 3) {
			return "不通过";
		}
	}
	
	
	/*异常原因，1海关清关不通过，2缺货，3海关人工审单，4海关抽验验货，5系统异常，6其他*/
	function exceptionType(value) {
		if (value == null || value == ''){
			return "未知";
		}else if(value == 1) {
			return "海关清关不通过";
		}else if(value == 2) {
			return "缺货";
		}else if(value == 3) {
			return "海关人工审单";
		}else if(value == 4) {
			return "海关抽验验货";
		}else if(value == 5) {
			return "系统异常";
		}else if(value == 6) {
			return "其他";
		}else{
			return "无";
		}
	} 
	
	function isnumber(value) {
		if (value == null || value == '')
			return "0.00";
		else
			return value;
	}
	
});